package com.example.springbootshiro.controller;

import com.example.springbootshiro.dao.UserMapper;
import com.example.springbootshiro.entity.BuildProduct;
import com.example.springbootshiro.entity.JsonResult;
import com.example.springbootshiro.entity.Office;
import com.example.springbootshiro.entity.User;
import com.example.springbootshiro.service.UserService;
import com.example.springbootshiro.shiro.ShiroRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.annotation.RequestScope;

import java.util.List;


@Controller
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/login")
    public String login(String name, String password, Model model){
        UsernamePasswordToken token = new UsernamePasswordToken(name, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            model.addAttribute("msg", "用户名不存在");
            return "/user/login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg", "密码错误");
            return "/user/login";
        }
        String uid = (String)subject.getSession().getAttribute("uid");
        User user = userService.selectOne(Long.parseLong(uid));
        if ("user:admin".indexOf(user.getPerms()) != -1){
            return "redirect:/product/manager";
        }

        return "redirect:/system";
    }

    @RequestMapping("/system")
    public String system(){
        return "/user/testThymeleaf";
    }

    @RequestMapping("/add")
    public String add(){
        return "/user/add";
    }
    @RequestMapping("/update")
    public String update(){
        return "/user/update";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "/user/login";
    }
    @RequestMapping("/logout")
    public String unAuth(){
        SecurityUtils.getSubject().logout();
        return "/user/login";
    }

    @RequestMapping("/user/list")
    public String list(){
        return "/user/list";
    }

    @RequestMapping("/user/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model){
        User user = userService.selectOne(id);
        model.addAttribute("user", user);
        return "/user/info";
    }

    @RequestMapping("/user/info")
    public String info(){
        return "/user/add";
    }

    @RequestMapping("/user/insert")
    @ResponseBody
    public JsonResult insert(@RequestBody User user){
        JsonResult jsonResult = new JsonResult();
        userService.add(user);
        jsonResult.setMsg("ok");
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    @RequestMapping("/user/all")
    @ResponseBody
    public JsonResult userAll(String param){
        JsonResult jsonResult = new JsonResult();
        List<User> users = userService.selectAll(param);
        jsonResult.setData(users);
        jsonResult.setMsg("ok");
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    @RequestMapping("/user/update")
    @ResponseBody
    public JsonResult update(@RequestBody User user){
        JsonResult jsonResult = new JsonResult();
        userService.update(user);
        jsonResult.setSuccess(true);
        jsonResult.setMsg("ok");
        return jsonResult;
    }

    @RequestMapping("/user/delete")
    @ResponseBody
    public JsonResult delete(String id){
        JsonResult jsonResult = new JsonResult();
        userService.delete(Long.parseLong(id));
        jsonResult.setSuccess(true);
        jsonResult.setMsg("ok");
        return jsonResult;
    }

    @RequestMapping("/product/manager")
    public String productManager(){
        return "/product/product-manger";
    }

    @RequestMapping("/office/product/list")
    @ResponseBody
    public JsonResult officeProductList(@RequestBody Office office){
        JsonResult jsonResult = new JsonResult();
        return jsonResult;
    }

    @RequestMapping("/build/product/list")
    @ResponseBody
    public JsonResult buildProductList(@RequestBody BuildProduct buildProduct){
        JsonResult jsonResult = new JsonResult();
        return jsonResult;
    }



}
